#ifndef TCP_CLIENT_H
#define TCP_CLIENT_H


#include <QObject>
#include <QTcpSocket>
#include <QByteArray>
#include <QTimer>


class TcpClient : public QObject
{
    Q_OBJECT
public:
    TcpClient(QObject *object, QString ip, int port);


    ~TcpClient();

private:
    /**
        QAbstractSocket::UnconnectedState：未连接状态
        QAbstractSocket::HostLookupState：正在解析主机名
        QAbstractSocket::ConnectingState：正在尝试连接
        QAbstractSocket::ConnectedState：已连接
        QAbstractSocket::BoundState：已绑定
        QAbstractSocket::ClosingState：正在关闭
        QAbstractSocket::ListeningState：正在监听
     */
    enum SocketState {
        UnconnectedState=0,
        HostLookupState=1,
        ConnectingState=2,
        ConnectedState=3,
        BoundState=4,
        ListeningState=5,
        ClosingState=6
    };

    QObject* m_object;

    QTcpSocket* m_tcpSocket;

    QByteArray m_receiveData;

    //定时重连服务器
    QTimer* m_reconnectTimer;
    //重连计时
    int m_refreshTime = 3000;

    QString m_ip;
    int m_port;

public slots:
    /**
     * @brief initializeFunction:创建套接字、计时器、功能槽函数
     */
    void initializeFunction();
    /**
     * @brief getConnectStatus：获得连接状态
     * @return
     */
    int getConnectStatus();
    /**
     * @brief startClientReconnectTcpServerTimer:开始重连计时器
     */
    void startClientReconnectTcpServerTimer();
    /**
     * @brief reConnectTcpServer：重连服务器
     * @return
     */
    bool reConnectTcpServer();
    /**
     * @brief sendData：发送有数据给服务器
     * @param data
     */
    void sendData(QByteArray data);
signals:
    void receiveData(QByteArray data);

    void startReConnectTcpServerTimer();

    void stopReConnectTcpServerTimer();
};
#endif // TCP_CLIENT_H
